<script lang="ts" setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import SearchCom from '@/components/SearchCom.vue'
import { getDepStuList } from '@/api/department-api.js'
import { useAuthStore } from '@/stores/auth'
const authStore = useAuthStore()

/*
{
    "id": 1,
    "departmentId": 1,
    "studentId": "2206631602",
    "departmentName": "计算机协会",
    "studentName": "范宇新"
}
*/

// 搜索
const searchText = ref('')
const curPage = ref(1)
const total = ref(0)

// 表格
const tableData = ref([])

// 更新数据
const loadData = async () => {
  const depId = authStore.userInfo['department']['id']
  const { data } = await getDepStuList(curPage.value, 10, searchText.value, depId)

  console.log(data)
  
  tableData.value = data.list
  total.value = data.total
}
loadData()

// 搜索和添加按钮
const handleSearch = async (text: string) => {
  searchText.value = text
  // 获取表单数据并发送请求
  loadData()
}


// 分页
const handlePagination = async (page: any, size: any) => {
  curPage.value = page
  loadData()
}

</script>


<template>
  <div class="wrap">
    <!-- 搜索和添加按钮 -->
    <SearchCom @search="(searchText) => handleSearch(searchText)"></SearchCom>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">

      <el-table-column label="ID" width="50">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="部门名称" width="120">
        <template #default="scope">
          {{ scope.row.departmentName }}
        </template>
      </el-table-column>

      <el-table-column label="学生姓名" width="200">
        <template #default="scope">
          {{ scope.row.studentName }}
        </template>
      </el-table-column>

      <el-table-column label="学号">
        <template #default="scope">
          {{ scope.row.studentId }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small">
            编辑
          </el-button>
          <el-popconfirm confirm-button-text="是" cancel-button-text="否" :icon="InfoFilled" icon-color="#626AEF"
            title="是否删除？">
            <template #reference>
              <el-button size="small" type="danger">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <div class="paginationWrap">
      <el-pagination @change="handlePagination" background layout="prev, pager, next" :total="total" />
    </div>
  </div>
</template>

<style scoped>
.paginationWrap {
  padding: 50px;
}
</style>